Merge "Fixing numeric sorting for numbers with leading zeros"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 20 Oct 2016 20:11:56 +0000 (20:11 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 20 Oct 2016 20:11:56 +0000 (20:11 +0000)
includes/collation/NumericUppercaseCollation.php

index 4bf2f73..d15daec 100644 (file)
@@ -36,11 +36,13 @@ class NumericUppercaseCollation extends UppercaseCollation {
                // shorter numbers before longer ones; if identical, then the characters will be compared, which
                // generates the correct results for numbers of equal length.
                $sortkey = preg_replace_callback( '/\d+/', function ( $matches ) {
-                       $len = strlen( $matches[0] );
+                       // Strip any leading zeros
+                       $number = ltrim( $matches[0], '0' );
+                       $len = strlen( $number );
                        // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte
                        // would allow only for 256, which doesn't feel future-proof.
                        $prefix = chr( floor( $len / 256 ) ) . chr( $len % 256 );
-                       return '0' . $prefix . $matches[0];
+                       return '0' . $prefix . $number;
                }, $sortkey );
 
                return $sortkey;